﻿using System;

namespace ForceSystem.Untangler
{
    public static class ReflectionHelper
    {
        static readonly char[] GenericSplit = { '`' };

        /// <summary>
        /// Get user friendly name from type.
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string GetDisplayName( Type type )
        {
            if (!type.IsGenericTypeDefinition &&
                !type.IsGenericType)
            {
                return type.Name;
            }

            // This is generic type definition, so we want to make display name like "class<T>"
            var result = type.Name.Split(GenericSplit)[0] + "<";
            var genericParamTypes = type.GetGenericArguments();
            for (var i = 0; i < genericParamTypes.Length; ++i)
            {
                if (i != 0) result += ",";
                result += GetDisplayName(genericParamTypes[i]);
            }

            return result + ">";
        }

    }
}
